// 路由鉴权：就是路由能否被访问到权限设置，全局守卫
// 引入路由器
import router from './router'
// 引入进度条
import Nprogress from 'nprogress'
// 进度条样式
import 'nprogress/nprogress.css'
// 隐藏进度条加载小球
Nprogress.configure({ showSpinner: false });
// 引入用户相关仓库
import useUserStore from './store/modules/user';
//引入大仓库
import pinia from '@/store'
let userStore = useUserStore(pinia);

// 用户未登录可以访问的路由
let whiteList = ["/home", '/hospital/register', '/hospital/detail', '/hospital/notice', '/hospital/close', '/hospital/search'];
// 全局守卫
router.beforeEach((to, from, next) => {
    // 动态设置网页标题
    document.title = to.meta.title
    // 访问路由组件之前 进度条开始动
    Nprogress.start()
    // 判断用户是否登录
    let token = userStore.userInfo.token
    if (token) {
        next()
    } else {
        if (whiteList.includes(to.path)) {
            next()
        } else {
            userStore.visiable = true
            next({ path: '/home', query: { redirect: to.fullPath } })
        }
    }
})
router.afterEach((to, from) => {
    // 访问路由组件之后 进度条结束
    Nprogress.done()
})